<?php

class Accident_record extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Accident_record':
	 * @var integer $id
	 * @var integer $fk_truck_id
	 * @var integer $fk_driver_id
	 * @var string $property_owner
	 * @var string $accident_date
	 * @var string $accident_place
	 * @var string $case_end_date
	 * @var string $accept_company
	 * @var string $agent
	 * @var string $compensation_received_date
	 * @var double $accident_cost
	 * @var double $compensation_amount
	 * @var double $cost_margin
	 * @var double $driver_share_rate
	 * @var double $ulti_loss_amount
	 * @var string $accident_detail
	 * @var integer $state
	 * @var string $create_time
	 */
	
	public $property_owner;
	public $plate_no;
	public $driver_name;
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'accident_record';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('property_owner','length','max'=>100),
			array('accident_place','length','max'=>50),
			array('accept_company','length','max'=>100),
			array('agent','length','max'=>30),
			array('accident_detail','length','max'=>500),
			array('fk_truck_id, fk_driver_id, state', 'numerical', 'integerOnly'=>true),
			array('accident_cost, compensation_amount, cost_margin, ulti_loss_amount', 'numerical'),
			array('property_owner,accident_date,accident_place,case_end_date,accept_company,agent,compensation_received_date,
			accident_cost,compensation_amount,cost_margin,driver_share_rate,ulti_loss_amount,accident_detail,plate_no,driver_name', 'required'),
			array('driver_share_rate', 'numerical','integerOnly'=>true,'min'=>0,'max'=>100),
			array('accident_cost,compensation_amount,cost_margin,ulti_loss_amount', 'match',
			'pattern' => '/^-?\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('accident_date', 'match','pattern' => '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/','message'=>'{attribute} 必须为日期型(年-月-日  时:分)'),
			array('compensation_received_date,case_end_date', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
		);
	}
	
	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}
	
	protected function afterValidate($on){
		if($this->hasErrors()) return;
		$this->driver_share_rate=$this->driver_share_rate/100;
	}
	
	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'fk_truck_id' => '车辆id',
			'plate_no' => '车牌号',
			'fk_driver_id' => '司机id',
			'driver_name' =>'司机姓名',
			'property_owner' => '产权单位',
			'accident_date' => '事故时间',
			'accident_place' => '事故地点',
			'case_end_date' => '结案时间',
			'accept_company' => '理赔受理公司',
			'agent' => '处理事故经办人',
			'compensation_received_date' => '赔付金额到账时间',
			'accident_cost' => '事故总费用(元)',
			'compensation_amount' => '理赔总费用(元)',
			'cost_margin' => '费用差额(元)',
			'driver_share_rate' => '司机承担比例(%)',
			'ulti_loss_amount' => '公司实际赔付金额(元)',
			'accident_detail' => '事故详情',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'plate_no'=>array('B',10),
			'driver_name'=>array('C',10),
			'property_owner'=>array('D',15),
			'accident_date'=>array('E',20),
			'accident_place'=>array('F',15),
			'case_end_date'=>array('G',15),
			'accept_company'=>array('H',15),
			'agent'=>array('I',15),
			'accident_cost'=>array('J',15),
			'ulti_loss_amount'=>array('K',22),
		);
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='property_owner';
		$params[]='plate_no';
		$params[]='driver_name';
		return $params;
	}
}